{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecf5de21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 输入下列文本，输出对应的子串\n",
    "# 输入abcdefghijklmnopqrstuvwxyz， 输出dgjmps\n",
    "# 输入0123456789， 输出 97531\n",
    "def extract_substring(s):\n",
    "    if s == 'abcdefghijklmnopqrstuvwxyz':\n",
    "        return s[3::3] # 从第四个字符开始，每隔4个字符提取一个字符\n",
    "    elif s == '0123456789':\n",
    "        s = s[::-1]  # 先将字符串反转\n",
    "        return s[1::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a22956f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 判断字符串结尾\n",
    "# 输入一个字符串，当输入字符串结尾是 “PY” 时，输出“YES”，否则输出“NO”\n",
    "s = input()\n",
    "if s.endswith(\"PY\"):\n",
    "    print(\"YES\")\n",
    "else:\n",
    "    print(\"NO\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3af32c6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 人生苦短我用python\n",
    "# 输入一个小于等于12的整数n，逐个输出字符串“人生苦短我用python”中前n个字符，每个字符后输出一个半角逗号和一个空隔。\n",
    "\n",
    "n = int(input())\n",
    "s = \"人生苦短我用python\"\n",
    "for i in range(n):\n",
    "    print(s[i], end=', ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee407485",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 字符串加密\n",
    "# 用户在一行中输入一个包括大小写字母和数字的字符串\n",
    "# 编程将其中的大写字母用字母表中该字母后的第5个字母替代\n",
    "# 小写字母用字母表中该字母后的第3个字母替代\n",
    "# 其他字符原样输出，实现字符串加密。\n",
    "\n",
    "# 注意：大写字母和小写字母的转换是循环的，即Z转换为E，z转换为c。\n",
    "\n",
    "s = input()\n",
    "\n",
    "# 用ASCII码进行转换\n",
    "# ord() 函数返回字符的 ASCII 数值\n",
    "# chr() 函数将 ASCII 数值转换为字符\n",
    "def convert(s):\n",
    "    s_new = ''  # 用于存储转换后的字符串\n",
    "    for char in s:\n",
    "        if char.isupper(): # 大写字母\n",
    "            if char <= 'V':\n",
    "                char = chr(ord(char) + 5)\n",
    "            else:\n",
    "                char = chr(ord(char) - 21)\n",
    "            s_new += char\n",
    "        elif char.islower(): # 小写字母\n",
    "            if char <= 'x':\n",
    "                char = chr(ord(char) + 3)\n",
    "            else:\n",
    "                char = chr(ord(char) - 23)\n",
    "            s_new += char\n",
    "        else:\n",
    "            s_new += char\n",
    "    return s_new\n",
    "\n",
    "print(convert(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1f11920",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 回文判断\n",
    "s = input()\n",
    "print(s == s[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea42bde6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6. 大小写转换\n",
    "s = input()\n",
    "s_new = \"\"\n",
    "for char in s:\n",
    "    if char.islower():\n",
    "        char = char.upper()\n",
    "        s_new += char\n",
    "    elif char.isupper():\n",
    "        char = char.lower()\n",
    "        s_new += char\n",
    "    else:\n",
    "        s_new += char\n",
    "print(s_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d180fc69",
   "metadata": {},
   "outputs": [],
   "source": [
    "def rev(x1):\n",
    "    if len(x1) == 1:\n",
    "        return x1\n",
    "    else:\n",
    "        if x1[0] not in ['+', '-']:\n",
    "            if x1.isdigit():\n",
    "                return x1[::-1] # 没有符号，直接反转 \n",
    "            else:\n",
    "                return \"error\"\n",
    "        else:\n",
    "            sign=x1[0]  #取符号\n",
    "            numstr=x1[1:]\n",
    "            if not numstr.isdigit():\n",
    "                return \"error\"\n",
    "            revnum=numstr[::-1]\n",
    "            revnum = x1[0] + revnum.strip('0') #去除revnum前面可能有的0\n",
    "            return revnum\n",
    "        \n",
    "x=input()\n",
    "if len(x)<1:\n",
    "    print(\"请输入一个整数（可带符号）\")\n",
    "else:\n",
    "    y=rev(x)\n",
    "    if y != \"error\":\n",
    "        print(\"该数的逆序数是：{}\".format(y))\n",
    "    else:\n",
    "        print(\"输入数据必须为数字！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cc32a2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9. 分类统计字符\n",
    "# 用户输入一个字符串，分别统计其中小写字母、大写字母、数字、空格和其他字符的个数，并在一行内输出小写字母、大写字母、数字、空格和其他字符的个数。\n",
    "\n",
    "str = input()\n",
    "lower = 0\n",
    "upper = 0\n",
    "num = 0\n",
    "space = 0\n",
    "other = 0\n",
    "for char in str:\n",
    "    if char.islower():\n",
    "        lower += 1\n",
    "    elif char.isupper():\n",
    "        upper += 1\n",
    "    elif char.isdigit():\n",
    "        num += 1\n",
    "    elif char.isspace():\n",
    "        space += 1\n",
    "    else:\n",
    "        other += 1\n",
    "print(lower, upper, num, space, other, sep=' ')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
